package com.itheima.controller.backend;


import com.itheima.common.DTO.CheckItemDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/checkitem")
@Slf4j
@Api(tags = "检查项管理")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;


    /**
     * 新增
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("添加检查项:{}",checkItem);
        if(checkItemService.add(checkItem) > 0){
            return new Result(true,"新增检查项成功");
        }else {
            return new Result(false,"新增检查项失败");
        }

    }


    /**
     * 删除检查项
     *
     * @return
     */
    @ApiOperation("删除检查项")
    @GetMapping("/delete")
    public Result delete(Integer id){
        log.info("删除检查项:{}", id);
        if(checkItemService.delete(id) > 0){
            return new Result(true,"删除检查项成功");
        }else {
            return new Result(false,"删除检查项失败");
        }
    }

    /**
     * 根据id查询检查项
     *
     * @return
     */
    @ApiOperation("根据检查组id查询检查项")
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("查询检查项:{}", id);
        CheckItem checkItem = checkItemService.findById(id);
        if(checkItem != null){
            return new Result(true,"查询检查项成功", checkItem);
        }else {
            return new Result(false,"查询检查项失败");
        }
    }

    /**
     * 编辑检查项
     *
     * @return Result
     */
    @PostMapping("/edit")
    @ApiOperation("编辑检查项")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项:{}", checkItem);
        if(checkItemService.edit(checkItem) > 0){
            return new Result(true,"编辑检查项成功");
        }else {
            return new Result(false,"编辑检查项失败");
        }
    }


    /**
     * 分页查询检查项
     *
     * @return Result
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询检查项")
    public PageResult findPage(@RequestBody CheckItemDTO checkItemDTO){
        log.info("分页查询检查项:{}", checkItemDTO);

        PageResult pageResult = checkItemService.pageQuery(checkItemDTO);
//        return new Result(true,"分页查询检查项成功",pageResult);
        return pageResult;
    }


    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据检查组checkgroupId查询检查项id")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        log.info("根据检查组id查询检查项id:{}", checkgroupId);
        try {
            return new Result(true,"根据检查组id查询检查项id成功",
                    checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"根据检查组id查询检查项id失败");
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        log.info("查询所有检查项");
        try {
            return new Result(true,"查询所有检查项成功",
                    checkItemService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询所有检查项失败");
        }
    }



}
